package com.panshi.service.impl;

import com.panshi.api.util.PageSizeVO;
import com.panshi.bean.*;
import com.panshi.config.EmailPhoneUtill;
import com.panshi.mapper.AnswerMapper;
import com.panshi.service.AnswerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import java.util.List;

@Service
public class AnswerServiceImpl implements AnswerService {
    @Resource
    private AnswerMapper mapper;

    @Autowired
    private EmailPhoneUtill emailPhoneUtill;

    public int initAnswer(AnswerNoBean ansNo) {//用户回答某个问题
        return mapper.initAnswer(ansNo);
    }

    //用户删除回答========================================================================================================
    public int updateQa(int qid) {//问题表的回答数-1
        return mapper.updateQa(qid);
    }
    public int updateQaAnswerNo(int aid) {//删除答案审核表的回答
        return mapper.updateQaAnswerNo(aid);
    }
    public int updateQaAnswer(int aid) {//删除答案表的问题
        return mapper.updateQaAnswer(aid);
    }
    public int updateQaAnswerCom(int aid) {//删除该答案下的所有评论
        return mapper.updateQaAnswerCom(aid);
    }
    public int updateQaAnswerLike(int aid) {//删除该答案下的所有点赞
        return mapper.updateQaAnswerLike(aid);
    }
    public int deleteAnswerCount(int uid) {//该用户的总回答数-1
        return mapper.deleteAnswerCount(uid);
    }
    public int deleteAnswerDynamic(int yewu_id) {//删除动态表数据
        return mapper.deleteAnswerDynamic(yewu_id);
    }


    public List<AnswerComBean> openCom(int aid, int pid) {//用户点击某个回答打开回复
        return mapper.openCom(aid,pid);
    }
    public int updateAnswer(AnswerBean2 answer) {//用户修改回答
        return mapper.updateAnswer(answer);
    }

    @Override
    public int updateAnswerNo(AnswerBean2 answer) {//用户修改未审核表回答
        return mapper.updateAnswerNo(answer);
    }


    //用户给回答点赞======================================================================================================
    public AnswerLikeBean findLike(int aaid,int uid) {//查询用户该回答是否点赞或踩过
        return mapper.findLike(aaid,uid);
    }
    public int clientLike(int aaid, int uid) {//用户给回答点赞
        return mapper.clientLike(aaid,uid);
    }
    public int clientNo(int aaid, int uid) {//用户给回答点踩
        return mapper.clientNo(aaid,uid);
    }
    public AnswerBean2 findAnswerLikeNo(int aaid, int uid) {//返回点赞和点踩数
        return mapper.findAnswerLikeNo(aaid,uid);
    }
    public int addLike(int aaid) {//回答表点赞+1
        return mapper.addLike(aaid);
    }
    public int addNo(int aaid) {//回答表点踩+1
        return mapper.addNo(aaid);
    }


    public IssBean FindbyQid(int qid) {//根据问题查找问题
        return mapper.FindbyQid(qid);
    }

    public AnswerBean2 findbyAid(int aid) {//根据回答id查询回答信息
        return mapper.findbyAid(aid);
    }
    public AnswerNoBean findbyAidNo(int aaid) {//根据回答id查询回答信息(审核表)
        return mapper.findbyAidNo(aaid);
    }


    //管理员接口==========================================================================================================
    @Cacheable(value = "answer",key="#p0.startIndex+#root.methodName+#p0.state")
    public List<AnswerBean2> adminFindAnswer(@RequestBody PageSizeVO pageSizeVO){//管理员查询回答表
        return mapper.adminFindAnswer(pageSizeVO);
    }
    @Cacheable(value = "answer",key="#p0.startIndex+#root.methodName+#p0.state")
    public List<AnswerNoBean> adminFindAnswerNo(PageSizeVO pageSizeVO){//管理员查询回答审核表
        return mapper.adminFindAnswerNo(pageSizeVO);
    }

    public int adminFindAnswerSum(int state) {//管理员查询回答表总记录数
        return mapper.adminFindAnswerSum(state);
    }
    public int adminFindAnswerNoSum(int state) {//管理员查询回答审核表总记录数
        return mapper.adminFindAnswerNoSum(state);
    }

    @CacheEvict(value="answer",allEntries=true)
    public int adminUpdateQaAnswer(int state,int aaid) {//管理员修改回答表
        return mapper.adminUpdateQaAnswer(state,aaid);
    }

    public int addQaInstation(AnswerNoBean answer) {//将答案存入站内消息表
        return mapper.addQaInstation(answer);
    }
    public int findDynamic(int aaid) {//判断动态表是否存在这个回答
        return mapper.findDynamic(aaid);
    }

    public int addDynamic(int aaid, int uid) {//将审核通过的回答存进动态表
        return mapper.addDynamic(aaid,uid);
    }

    @CacheEvict(value="answer",allEntries=true)
    public int adminInitAnswer(AnswerNoBean answer) {//管理员审核回答进入回答表
        mapper.adminInitAnswer(answer);
        return answer.getAaid();
    }

    public int updateUserData(int uid) {//给用户表的回答数+1
        return mapper.updateUserData(uid);
    }

    @CacheEvict(value="answer",allEntries=true)
    public int adminAnswerNo(AnswerNoBean answer) {//管理员修改审核表
        return mapper.adminAnswerNo(answer);
    }






    //审核通过给用户发送邮箱问题提醒=========================================================================================
    public void setemailanswer(Integer uid, Integer state, String title) throws javax.mail.MessagingException {
        BeiUser beiUser = mapper.selectById(uid);//查询用户
        if(StringUtils.isEmpty(beiUser.getEmail())){
            return ;
        }
        String message="";
        if(state==1){
            message ="<p style='color:red'>你好</p><p>恭喜你 你在渣男口腔网回答的内容:</p>《"+title+"》<p>审核通过 已上架</p>";
            emailPhoneUtill.setemailarticle(beiUser.getEmail(),message);
        }else if(state==2){
            message ="<p style='color:red'>你好</p><p>很遗憾 你在渣男口腔网回答的内容:</p>《"+title+"》<p>违反国家法律规定 已被强制下架 请修改内容后重新发布</p>";
            emailPhoneUtill.setemailarticle(beiUser.getEmail(),message);
        }
    }

    public void setemailanswerno(Integer uid, Integer state, String title) throws MessagingException {
        BeiUser beiUser = mapper.selectById(uid);//查询用户
        if(StringUtils.isEmpty(beiUser.getEmail())){
            return ;
        }
        String message="";
        if(state==2){
            message ="<p style='color:red'>你好</p><p>很遗憾 你在渣男口腔网回答的内容:</p>《"+title+"》<p>违反国家法律规定 审核不通过 请修改内容后重新发布</p>";
            emailPhoneUtill.setemailarticle(beiUser.getEmail(),message);
        }else if(state==3){
            message ="<p style='color:red'>你好</p><p>恭喜你 你在渣男口腔网回答的内容:</p>《"+title+"》<p>审核通过</p>";
            emailPhoneUtill.setemailarticle(beiUser.getEmail(),message);
        }
    }
}
